package com.keyroy.game.puzzle_rpg.views.tables;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.ResourceManageX;
import org.eclipse.wb.swt.SWTResourceManager;

import com.keyroy.game.puzzle_rpg.dialogs.SkillSelector;
import com.keyroy.game.puzzle_rpg.models.environment.Project;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.role.Role;
import com.keyroy.game.puzzle_rpg.models.skill.Skill;
import com.keyroy.game.puzzle_rpg.models.skill.SkillIndex;
import com.keyroy.game.puzzle_rpg.models.skill.SkillSet;
import com.keyroy.game.puzzle_rpg.utils.UIUtil;
import com.keyroy.game.puzzle_rpg.views.listener.TableListener;

public class RoleSkillTableView extends Composite {
	private List<TableListener> tableListeners = new ArrayList<TableListener>();
	private Table table;
	private Role role;

	public RoleSkillTableView(Composite parent) {
		super(parent, SWT.NONE);
		setLayout(new FormLayout());

		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.left = new FormAttachment(0);
		fd_table.right = new FormAttachment(100);
		fd_table.top = new FormAttachment(0);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("\u540D\u79F0");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("\u6548\u679C");

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		fd_table.bottom = new FormAttachment(100, -39);
		FormData fd_toolBar = new FormData();
		fd_toolBar.bottom = new FormAttachment(100, -10);
		fd_toolBar.left = new FormAttachment(0, 10);
		toolBar.setLayoutData(fd_toolBar);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (role != null) {
					UIUtil.open(new SkillSelector(getDisplay(), new EventListener<Skill>() {

						@Override
						public void onEvent(Skill skill) {
							role.skillIndex.add(skill);
							reset();
						}
					}));
				}
			}
		});
		tltmNewItem.setImage(SWTResourceManager.getImage(RoleSkillTableView.class, "/res/add_obj.gif"));
		tltmNewItem.setText("\u6DFB\u52A0");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (role != null && table.getSelectionIndex() != -1) {
					role.skillIndex.remove(table.getSelectionIndex());
				}

			}
		});
		tltmNewItem_1.setImage(SWTResourceManager.getImage(RoleSkillTableView.class, "/res/remove_correction.gif"));
		tltmNewItem_1.setText("\u5220\u9664");

	}

	public void set(Role role) {
		this.role = role;
		reset();
	}

	public void reset() {
		table.removeAll();
		tableListeners.clear();
		if (role != null) {
			SkillSet skillSet = Project.getProject().getSkillSet();
			SkillIndex skillIndex = role.skillIndex;
			for (int i = 0; i < skillIndex.size(); i++) {
				Skill skill = skillIndex.get(skillSet, i);
				addTableItem(skill);
			}
		}
	}

	public void addTableItem(Skill skill) {
		if (skill != null) {
			TableItem tableItem = new TableItem(table, SWT.NONE);
			if (skill.icon != null) {
				tableItem.setImage(ResourceManageX.loadImage(skill.icon, 16, 16, false));
			}
			tableItem.setText(skill.name);
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	protected final String stringOf(String text) {
		if (text == null) {
			return "";
		} else {
			return text;
		}
	}
}
